package edu.tjut.tjutcommunity.controller;


import edu.tjut.tjutcommunity.entity.*;
import edu.tjut.tjutcommunity.exception.PicNotFoundException;
import edu.tjut.tjutcommunity.service.PostService;
import edu.tjut.tjutcommunity.service.PostlogsService;
import edu.tjut.tjutcommunity.service.UserService;
import edu.tjut.tjutcommunity.utils.UserUtil;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yhh
 * @since 2022-04-16
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/post")
public class PostController {

    final
    PostService postService;

    final
    UserService userService;
    final
    PostlogsService postlogsService;


    public PostController(PostService postService, UserService userService, PostlogsService postlogsService) {
        this.postService = postService;
        this.userService = userService;
        this.postlogsService = postlogsService;
    }

    /**
     * 根据pid获取指定的帖子信息。
     *
     * @param pid
     * @return
     */
    @GetMapping("/{pid}")
    public Message getPost(@PathVariable String pid) {
        User user = null;
        try {
            String stuId = UserUtil.getCurStuId();
            user = userService.getUserByStuId(stuId);
        } catch (Exception ignored) {

        }
        Post post = postService.getPostById(pid);
        if (post != null) {
            //假如用户信息不为空,添加用户浏览信息
            if (user != null) {
                //如果是从推荐来的，需要将recommend的feedback置为已浏览。
                postlogsService.insert(user, Integer.valueOf(pid), 0);
            }
            return Message.ok(post);
        }
        return Message.empty();
    }


    /**
     * 获取帖子
     *
     * @param timestamp 根据时间获取早于该时间的帖子
     * @param keyword   搜索关键字
     * @param pageSize  帖子数量
     * @param type      帖子类型
     * @return 结果
     */
    @PostMapping("/post")
    public Message post(@RequestParam(value = "timestamp", required = false) Long timestamp,
                        @RequestParam(value = "keyword", required = false) String keyword,
                        @RequestParam(value = "pageSize", required = false) Integer pageSize,
                        @RequestParam(value = "type", required = false) Integer type) {
        if (timestamp == null) timestamp = new Date().getTime();
        PageResult<Post> result = postService.getPostByTimeAndPageSizeAndKey(keyword, pageSize, new Date(timestamp), type);
        if (result.getResult().isEmpty()) return Message.empty();
        return Message.ok(result);
    }

    @PostMapping("/searchPost")
    public Message postByKeyWords(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "pageNo", required = false) Integer pageNo) {
        if (pageNo == null) pageNo = 1;
        PageResult<Post> result = postService.getPostByPageAndKey(keyword, pageSize, pageNo, type);
        if (result.getResult().isEmpty()) return Message.empty();
        return Message.ok(result);
    }


    /**
     * 多图片上传
     *
     * @param post
     * @param ordinal
     * @return
     */
    @PostMapping("/publishPostImages")
    public Message publishPost(@RequestBody Post post, @RequestParam("ordinal") Integer ordinal) {
        return Message.failed();
    }

    @PostMapping("/publishPost")
    public Message publishPost(@RequestBody Post post) {
        String stuId = UserUtil.getCurStuId();
        User user = userService.getUserByStuId(stuId);
        if (postService.addPost(post, user)) {
            return Message.ok(post);
        }
        return Message.failed();
    }

    @GetMapping("/like/{pid}")
    public Message like(@PathVariable Integer pid) {
        String stuId = UserUtil.getCurStuId();
        User user = userService.getUserByStuId(stuId);
        user.setStudentId(stuId);
        //在此处通知被赞者
        if (postService.likeInc(pid, user)) {
            postlogsService.insert(user, pid, 2);
            return Message.ok();
        }
        return Message.failed();
    }

    @GetMapping("/del/{pid}")
    public Message delPost(@PathVariable Integer pid) {
        String stuId = UserUtil.getCurStuId();
        User user = userService.getUserByStuId(stuId);
        if (!postService.delPost(pid, user)) {
            return Message.failed("删除失败");
        }
        return Message.ok("删除成功");
    }
    @GetMapping("/ismypost/{pid}")
    public Message ismypost(@PathVariable Integer pid){
        String stuId = UserUtil.getCurStuId();
        User user = userService.getUserByStuId(stuId);
        if (!postService.isMyPost(pid, user)) {
            return Message.ok("no");
        }
        return Message.ok("yes");
    }
    @GetMapping("/getmyposts")
    public Message getmyposts(){
        String stuId = UserUtil.getCurStuId();
        User user = userService.getUserByStuId(stuId);
        List<Integer> result = postService.getMyPosts(user.getUid());
        return Message.ok(result);
    }

    @Deprecated
    @PostMapping("/recommend")
    public Message getRecommend(@RequestParam(value = "pageNo", required = false) Integer pageNo, @RequestParam(value = "drive", required = false) Integer drive) {
        User user = userService.getUserByStuId(UserUtil.getCurStuId());
        if (user == null) {
            //热点推荐
            return Message.empty();
        } else {
            pageNo = pageNo == null ? 0 : pageNo;
            //获取推荐的帖子
            PageResult<Post> recommendPost = postService.getRecommendPost(user, drive, pageNo, 5);
            if (recommendPost.getResult().isEmpty()) return Message.empty();
            return Message.ok(recommendPost);
        }
    }


}

